<?php

namespace app\api\controller;
use think\Db;
use app\api\model\WxappPage;

/**
 * 页面控制器
 * Class Index
 * @package app\api\controller
 */
class Page extends Controller
{
    /**
     * 首页diy数据
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function home()
    {
        // 页面元素
        $items = WxappPage::getPageItems($this->getUser(false));
        return $this->renderSuccess(compact('items'));
    }

    /**
     * 自定义页数据
     * @param $page_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function custom($page_id)
    {
        // 页面元素
        $items = WxappPage::getPageItems($this->getUser(false), $page_id);
        return $this->renderSuccess(compact('items'));
    }
    //获取首页信息
    public function category()  {
         $token=request()->param('token');
          if(!empty($token)){
          $where['grade']=['<=',$this->getUser()['grade']];
          }else{
           $where['grade']=['>=',0];
          }
     
      
        $list=Db::name('category')->where('parent_id',0)->field('category_id,name')->select();
        $goods=Db::name('goods')->where('is_delete',0)->where($where)->limit(200)->select()->each(function($item,$key){
            $item['type']=$this->gettype($item['goods_jianjie']);
            $item['region']=mb_substr($this->getregion($item['pianqu']),0,1,'utf-8');
            $item['xiaoqu']=mb_substr($this->gethouse($item['xiaoqu']),0,1,'utf-8'); 
            return $item;
        });
        $region=Db::name('category')->where('parent_id',10001)->field('category_id,name')->select()->toArray();
        $regiondata['category_id']=0;
        $regiondata['name']='全部';
        array_unshift($region, $regiondata);


        $type=Db::name('type')->field('id,name')->select()->toArray();
        $typedata['id']=0;
        $typedata['name']='全部';
        array_unshift($type, $typedata);
      
      
        $house=Db::name('house')->field('id,name')->select()->toArray();
        $housedata['id']=0;
        $housedata['name']='全部';
        array_unshift($house, $housedata);

        $money=Db::name('money')->field('id,name,parent_id')->select()->each(function($item,$key){
            $item['money']=$item['name'].'-'.$item['parent_id'];
            unset($item['name']);
            unset($item['parent_id']);
            return $item;
        })->toArray();
        $moneydata['id']=0;
        $moneydata['money']='全部';
        array_unshift($money, $moneydata);


        return $this->renderSuccess(compact('list','type','region','money','goods','house'));

    }

    //房型
    public function gettype($id){
        $list=Db::name('type')->where('id',$id)->find();
        return $list['name'];
    }

    //片区
    public function getregion($id){
        $list=Db::name('category')->where('category_id',$id)->find();
        return $list['name'];
    }


    public function getgoods($id){
        $list=Db::name('goods')->where('goods_id',$id)->find();
        $list['type']=$this->gettype($list['goods_jianjie']);
        $list['xiaoqu']=$this->gethouse($list['xiaoqu']);
        $list['region']=$this->getregion($list['pianqu']);

        return $this->renderSuccess(compact('list'));
    }

    //获取小区
    public function gethouse($id){
        $list=Db::name('house')->find($id);
        return $list['name'];

    }

}
